package com.sxw.hw_student.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxw.hw_student.system.entity.SysRole;
import com.sxw.hw_student.system.entity.SysUserRole;
import com.sxw.hw_student.system.mapper.SysRoleMapper;
import com.sxw.hw_student.system.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxw.hw_student.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.LambdaConversionException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sxw
 * @since 2025-04-02
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    ISysUserRoleService sysUserRoleService;

    /**
     * 2在实现类中实现service层中定义的抽象方法
     *
     * @param sysRole
     */
    @Override
    public void testRoleAdd(SysRole sysRole) {
//    将新增的角色保存到角色表
        this.save(sysRole);
//    将新增的角色保存到角色用户中间表（为超级管理员添加对应关系）
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(sysRole.getRoleId());
        sysUserRole.setUserId(1);
        sysUserRoleService.save(sysUserRole);
    }

    @Override
    public Page<SysRole> queryRolePage(Page<SysRole> page, String roleName) {

//    构造查询条件（创建一个查询条件的实例）
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();


//    如果roleName（查询调剂）为空，则不添加查询条件；如果不为空，则添加查询条件
        if (StringUtils.isNotBlank(roleName)) {
            queryWrapper.like(SysRole::getRoleName, roleName);
//        queryWrapper.like("user_name",userName);
        }
//    调用mybaties-plus的分页方法，传入查询条件和分页参数，返回分页查询后的结果
        return this.page(page, queryWrapper);

    }

    @Override
    public boolean update(SysRole sysRole) {
        return false;
    }

    @Service
    public class SysRoleService {
        @Autowired
        private SysRoleMapper sysRoleMapper;

        public SysRole getById(Long id) {
            return sysRoleMapper.selectById(id);
        }

        public boolean update(SysRole sysRole) {
            return sysRoleMapper.update(sysRole) > 0;
        }
    }



}
